package com.dfec.book.controller;


import com.dfec.book.entity.BorrowRecordEntity;
import com.dfec.book.service.BorrowRecordService;
import com.dfec.book.util.AjaxResult;
import com.dfec.book.util.PageUtils;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * 借阅记录管理
 * @author tangrg
 * @email 1446232546@qq.com
 * @date 2025-11-19 11:39:24
 */
@RestController
@RequestMapping("/borrowRecord")
@Api(tags = "借阅记录管理")
@ApiSort(3)
public class BorrowRecordController {

    @Resource
    private BorrowRecordService borrowRecordService;

    /**
     * 分页查询借阅记录
     */
    @PostMapping("/page")
    @ApiOperation("分页查询借阅记录")
    public AjaxResult<PageUtils> getRecordsByPage(
            @ApiParam(value = "借阅记录查询参数", required = true)
            @RequestBody BorrowRecordEntity borrowRecord) {

        PageUtils page = borrowRecordService.getRecordsByPage(borrowRecord);
        return AjaxResult.ok(page);
    }

    /**
     * 借阅图书
     */
    @PostMapping("/borrow")
    @ApiOperation("借阅图书")
    public AjaxResult<Boolean> borrowBook(
            @ApiParam(value = "借阅记录实体", required = true)
            @RequestBody BorrowRecordEntity record) {

        try {
            boolean result = borrowRecordService.borrowBook(record);
            return result ? AjaxResult.ok() : AjaxResult.error("借阅失败");
        } catch (RuntimeException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 归还图书
     */
    @GetMapping("/return")
    @ApiOperation("归还图书")
    public AjaxResult<Boolean> returnBook(
            @ApiParam(value = "借阅记录ID", required = true)
            @RequestParam Integer recordId) {

        try {
            boolean result = borrowRecordService.returnBook(recordId);
            return result ? AjaxResult.ok() : AjaxResult.error("归还失败");
        } catch (RuntimeException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 获取借阅人的所有借阅记录
     */
    @GetMapping("/byBorrower")
    @ApiOperation("获取借阅人的所有借阅记录")
    public AjaxResult<List<BorrowRecordEntity>> getRecordsByBorrower(
            @ApiParam(value = "借阅人姓名", required = true)
            @RequestParam String borrower) {

        List<BorrowRecordEntity> records = borrowRecordService.getRecordsByBorrower(borrower);
        return AjaxResult.ok(records);
    }

    /**
     * 获取某图书的借阅记录
     */
    @GetMapping("/byBook")
    @ApiOperation("获取某图书的借阅记录")
    public AjaxResult<List<BorrowRecordEntity>> getRecordsByBook(
            @ApiParam(value = "图书ID", required = true)
            @RequestParam Integer bookId) {

        List<BorrowRecordEntity> records = borrowRecordService.getRecordsByBook(bookId);
        return AjaxResult.ok(records);
    }

    /**
     * 获取当前借出未归还的记录
     */
    @GetMapping("/unreturned")
    @ApiOperation("获取当前未归还的借阅记录")
    public AjaxResult<List<BorrowRecordEntity>> getUnreturnedRecords() {
        List<BorrowRecordEntity> records = borrowRecordService.getUnreturnedRecords();
        return AjaxResult.ok(records);
    }



    /**
     * 获取借阅记录详情
     */
    @GetMapping("/detail")
    @ApiOperation("获取借阅记录详情")
    public AjaxResult<BorrowRecordEntity> getRecordDetail(
            @ApiParam(value = "借阅记录ID", required = true)
            @RequestParam Integer recordId) {

        BorrowRecordEntity record = borrowRecordService.getById(recordId);
        return record != null ? AjaxResult.ok(record) : AjaxResult.error("记录不存在");
    }
}